<template>
    <div>
        <div>
            <el-form ref="searchForm" :inline="true" :model="condition.search" label-suffix="" label-width="100px"
                     label-position="left" size="small">
                <el-row>
                    <el-col>
                        <el-form-item>
                            <el-button type="primary" v-on:click="getData">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="exportExcel">导出</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="warning" @click="coverShowFlag = !coverShowFlag">
                                {{ coverShowFlag ? '隐藏封面' : '显示封面' }}
                            </el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <el-table
            id="mainTable"
            :data="tableData"
            size="small"
            :default-sort = "{prop: 'saleCount', order: 'descending'}"
            header-cell-class-name="tableHeaderStyle"
            :row-class-name="tableRowClassName">
            <el-table-column label="序号" width="60">
                <template v-slot="scope">
                    <span>{{ (condition.page - 1) * condition.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="title"
                label="标题">
            </el-table-column>
            <el-table-column
                prop="picUrl"
                v-if="coverShowFlag"
                label="封面">
                <template v-slot="scope">
                    <el-image style="width: 80px;"
                              :src="scope.row.picUrl"
                              :preview-src-list="picUrls"
                              fit="contain">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                prop="beginDate"
                label="开始时间">
            </el-table-column>
            <el-table-column
                prop="dealType"
                :formatter="formatDealType"
                label="类型">
            </el-table-column>
            <el-table-column
                prop="saleCount"
                sortable
                label="半年售">
            </el-table-column>
            <el-table-column
                prop="marketPrice"
                sortable
                label="市场价格">
            </el-table-column>
            <el-table-column
                prop="price"
                label="售卖价格">
            </el-table-column>
            <el-table-column
                prop="discountStr"
                label="折扣">
            </el-table-column>
            <el-table-column
                prop="onlineFlag"
                :formatter="formatOnlineFlag"
                label="是否在线">
            </el-table-column>
            <el-table-column
                prop="fee"
                label="点评佣金">
            </el-table-column>
            <el-table-column
                prop="syncDate"
                label="同步日期">
            </el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间">
            </el-table-column>
        </el-table>
        <el-pagination
            background
            small
            :hide-on-single-page="true"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="condition.page"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size.sync="condition.rows"
            layout="sizes, prev, pager, next"
            :total.sync="condition.total">
        </el-pagination>

        <el-divider content-position="left">数据变化历史</el-divider>
        <dp-tuan-history :shop-id="shopId"></dp-tuan-history>
    </div>
</template>

<script>
import {addDpTuan, deleteDpTuan, getDpTuanByPage, updateDpTuan} from '@/api/api'
import DpTuanHistory from "@/component/DpTuanHistory";

export default {
    name: "DpTuan",
    components: {DpTuanHistory},
    data() {
        return {
            // 分页搜索条件
            condition: {
                page: 1,
                rows: 10,
                total: 0,
                search: {}
            },
            // 表格数据
            tableData: [],
            // 新增或者编辑对话框显示
            addOrUpdateDialogVisible: false,
            // 标题
            addOrUpdateDialogTitle: '新增',
            // 新增或者编辑对话框表单
            addOrUpdateForm: {},
            // 选中行
            selectedRow: {},

            // 图片数组
            picUrls: [],

            // 默认不显示封面
            coverShowFlag: false,
        }
    },
    // 这里定义需要父组件传过来的东西
    props: {
        shopId: String,
        size: {
            type: String,
            default: 'small'
        },
    },
    methods: {
        // 这里可以单独设置每一行的样式
        tableRowClassName({row, rowIndex}) {
            return '';
        },
        handleSizeChange(val) {
            this.getData();
        },
        handleCurrentChange(val) {
            this.getData();
        },
        // 格式布尔值
        formatBoolean: function (row, column, cellValue) {
            if (cellValue) {
                return "是";
            } else {
                return "否";
            }
        },
        formatDealType: function (row, column, cellValue) {
            if (cellValue == '1') {
                return "团购";
            } else if (cellValue == '2') {
                return "代金券";
            } else {
                return "未知";
            }
        },
        formatOnlineFlag: function (row, column, cellValue) {
            if (cellValue == '0') {
                return "已下线";
            } else if (cellValue == '1') {
                return "在线";
            } else {
                return "未知";
            }
        },
        showAddDialog: function () {
            this.addOrUpdateDialogTitle = '新增';
            this.addOrUpdateDialogVisible = true;
            this.addOrUpdateForm = {};
        },
        showUpdateDialog: function (row) {
            this.addOrUpdateDialogTitle = '编辑';
            this.addOrUpdateDialogVisible = true;
            this.selectedRow = row;
            // 深拷贝
            this.addOrUpdateForm = Object.assign({}, row);
        },
        reset: function () {
            this.$refs['searchForm'].resetFields();
        },
        exportExcel: function () {
            this.GLOBAL.exportExcel('#mainTable', '数据.xlsx');
        },
        add: function () {
            addDpTuan(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        update: function () {
            updateDpTuan(this.addOrUpdateForm).then(data => {
                this.addOrUpdateDialogVisible = false;
                this.getData();
            });
        },
        onDelete: function (row) {
            this.$confirm('请确认是否删除, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteDpTuan({id: row.id}).then(data => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getData();
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        addOrUpdateConfirm: function () {
            if (this.addOrUpdateDialogTitle == '新增') {
                this.add();
            } else if (this.addOrUpdateDialogTitle == '编辑') {
                this.update();
            }
        },
        // 获取表格数据
        getData: function () {
            if (this.shopId) {
                this.condition.search.shopId = this.shopId;
            }
            getDpTuanByPage(this.condition).then(data => {
                this.tableData = data.body.rows;
                this.condition.total = data.body.total;

                if (this.tableData) {
                    this.tableData.forEach((item, index, arr) => {
                        item.picUrl = item.picUrl.split("%")[0];
                        this.picUrls.push(item.picUrl);
                    })
                }
            });
        },
    },
    created() {
        this.getData();
    },
}
</script>

<style scoped>
.el-input {
    width: 150px !important;
}

.el-select {
    width: 150px !important;
}
</style>
